/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bioteam.appweb.datatype.ParameterMapBuilder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;

//TODO implement this only needed
public class MobyTypeTree extends HttpServlet
{
	public static final Log logger = LogFactory.getLog(MobyTypeTree.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{

		if (req.getSession(true).getAttribute("user") == null)
		{
			
			ErrorSender.sendErrorForTreeLoader(400, "Authenication is required to retrive Application trees", resp);
			return;
		}

		String path = req.getParameter("text");

		try
		{
			String response = null;
			JSONArray ja = new JSONArray();
			if (path.equals("alltypes"))
			{
//				for (DataTypeNode childNode : MobyTypeCache.rootNode.getSubTypes())
//				{
//					ja.put(ParameterMapBuilder.dataNodeToJSON(childNode));
//				}
				response = ja.toString();
			} else if (path.equals("usedtypes"))
			{
				response=ParameterMapBuilder.usedTypesJSON;
			}
			
			resp.setStatus(HttpServletResponse.SC_OK);
			resp.setContentLength(response.length());
			resp.getWriter().write(response);

		} catch (Exception e)
		{

			logger.warn("error when retrieve moby type tree", e);
			ErrorSender.sendErrorForTreeLoader(500, e.getMessage(), resp);
			return;
		}

	}

	

	
}
